﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySkiPatrolSite.MembershipSvc;

namespace MySkiPatrolSite
{
    public static class Security
    {
        public static bool IsLoggedIn(Member data)
        {
            bool myReturn = false;

            if(data!=null)
            {
                if(!string.IsNullOrEmpty(data.ID))
                {
                    myReturn = true;
                }
            }

            return myReturn;
        }

        

        public static string GetDbPrefix(AdminSvc.Patrol patrol)
        {
            return patrol.dbPrefix;
        }

        public static bool IsInRole(string role, Member member)
        {
            bool myReturn = false;

            try
            {
                if (member != null)
                {
                    if (member.Roles != null)
                    {
                        foreach (Role item in member.Roles)
                        {
                            if (item.Name.ToString().ToUpper() == role.ToString().ToUpper())
                            {
                                myReturn = true;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                myReturn = false;
            }

            return myReturn;
        }

        public static bool IsInGroup(string group, Member member)
        {
            bool myReturn = false;

            try
            {
                if (member != null)
                {
                    if (member.Groups != null)
                    {
                        foreach (Group item in member.Groups)
                        {
                            if (item.Name.ToString().ToUpper() == group.ToString().ToUpper())
                            {
                                myReturn = true;
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                myReturn = false;
            }

            return myReturn;
        }

        public static string GetSiteName(AdminSvc.Patrol patrol)
        {
            string myReturn = string.Empty;

            try
            {
                if (patrol != null)
                {
                    if (patrol.ApplicationSettings != null)
                    {
                        foreach (MySkiPatrolSite.AdminSvc.AppSetting item in patrol.ApplicationSettings)
                        {
                            if (item.Name.ToUpper() == "SITENAME")
                            {
                                myReturn = item.Value.ToUpper();
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }

            return myReturn;
        }
    }
}